package com.yq.testmodule.emoji

import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.ImageView
import android.widget.LinearLayout
import com.yq.testmodule.R

class CommentInputWidget @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : LinearLayout(context, attrs) {
    val inputManager: InputMethodManager
    val etInput: EmojiEditText

    init {
        addView(
            LayoutInflater.from(context).inflate(R.layout.comment_input_area, null), LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            )
        )
        inputManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        val ivEmoji = findViewById<ImageView>(R.id.ivEmoji)
        val ivKeyboard = findViewById<ImageView>(R.id.ivKeyBoard)
        etInput = findViewById(R.id.etInput)
        ivEmoji.setOnClickListener {
            mEmojiClickListener?.invoke()
            ivEmoji.visibility = GONE
            ivKeyboard.visibility = VISIBLE
        }


        ivKeyboard.setOnClickListener {
            mKeyboardClickListener?.invoke()
            ivKeyboard.visibility = GONE
            ivEmoji.visibility = VISIBLE
        }

        etInput.setOnClickListener {
            ivKeyboard.visibility = GONE
            ivEmoji.visibility = VISIBLE
            mEtInputClickListener?.invoke()
        }
    }


    //表情icon点击
    private var mEmojiClickListener: (() -> Unit)? = null
    fun addEmojiClickLister(emojiClick: () -> Unit) {
        mEmojiClickListener = emojiClick
    }


    //键盘icon点击
    private var mKeyboardClickListener: (() -> Unit)? = null
    fun addKeyboardClickLister(keyboardClick: () -> Unit) {
        mKeyboardClickListener = keyboardClick
    }

    //输入区域点击
    private var mEtInputClickListener: (() -> Unit)? = null
    fun addEtInputClickListener(inputClick: () -> Unit) {
        mEtInputClickListener = inputClick
    }

    /**
     * hide soft keyboard
     */
    fun hideSoftKeyboard() {
        if (etInput == null) {
            return
        }
        etInput.requestFocus()
        val activity = (context as Activity)
        if (activity.window.attributes.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
            if (activity.currentFocus != null) {
                inputManager.hideSoftInputFromWindow(
                    activity.currentFocus!!.windowToken, InputMethodManager.HIDE_NOT_ALWAYS
                )
            }
        }
    }

    /**
     * show soft keyboard
     * @param et
     */
    fun showSoftKeyboard() {
        if (etInput == null) {
            return
        }
        etInput.requestFocus()
        inputManager.showSoftInput(etInput, InputMethodManager.SHOW_IMPLICIT)
    }
}